<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ page import="java.util.*,java.net.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
//读取保存的Cookie，并与application中的用户进行匹配
	String name="";
	String pwd="";
	Cookie[] Cookies=request.getCookies();
	for(int i=0;i<Cookies.length;i++){
		
		if(Cookies[i].getName().equals("name")){	//在CheckLogin中寻找存name的Cookie
			name=Cookies[i].getValue();
			name=URLDecoder.decode(name,"utf-8");
		}
		if(Cookies[i].getName().equals("pwd")){		//在CheckLogin中寻找存pwd的Cookie
			pwd=Cookies[i].getValue();
		}
	}
	
	if(name.length()>0){
		@SuppressWarnings("unchecked")
		HashMap<String,String> map=(HashMap<String,String>)application.getAttribute("USERS");
		
		if(map.containsKey(name)){
			String mpwd=map.get(name);
			if(mpwd.equals(pwd)){
				request.setAttribute("NAME",name);
				response.sendRedirect("welcome.jsp");
				return;
			}
		}
	}
%>

<form action="CheckLogin" method="post">
<table id="f1">
<tr><td>用户名</td><td><input type="text" name="username" id="username"></td></tr>
<tr><td>密码</td><td><input type="password" name="pwd" id="pwd"></td></tr>
<tr><td colspan="2"><input type="submit" id="btn" value="提交"></td></tr>
</table>
</form>
</body>
</html>